package Model;
import java.util.*;

public class AddSubTaskCommand implements Command
{
   //attributes
   private ComplexTask ParentTask;
   private ArrayList<Task> TasksPointer;
   private ArrayList<Task> SubTasksPointer;
   private Task NewSubTask;
   private boolean undone;

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

 
	public AddSubTaskCommand(ArrayList<Task> PointerToTasks)
	{
		TasksPointer=PointerToTasks;
        undone=false;
	}

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void execute(String parentTask, String taskName, String iPeriodic, String periodicFrequency, Date iDate) throws Exception //iPeriodic should be either "Single" OR "Periodic"
    {                                                                                                   //periodicFrequency should be either Daily, Weekly or Monthly or null
        boolean wError = true;
    	if(ParentTask == null)
		{
			for(int i = 0; i < TasksPointer.size(); ++i)
			{
				if(TasksPointer.get(i).getName().equals(parentTask) && TasksPointer.get(i).getType().equals("Complex"))
				{
					wError = false;
					ParentTask = (ComplexTask)TasksPointer.get(i); //Casted.
                    SubTasksPointer = ParentTask.getTodo();

                    if(iPeriodic.equals("Single"))
                    {
                        SingleTask TemporaryPointer = new SingleTask(taskName,iDate,iPeriodic);
                        ParentTask.addTodoItem(TemporaryPointer);
                        NewSubTask=TemporaryPointer;
                        break;
                    }
                    else if(iPeriodic.equals("Periodic"))
                    {
                        PeriodicTask TemporaryPointer = new PeriodicTask(taskName,ParentTask.getDueDate(),iPeriodic,periodicFrequency);
                        ParentTask.addTodoItem(TemporaryPointer);
                        NewSubTask=TemporaryPointer;
                        break;
                    }
                    else
                    {
                    	throw new Exception("Thou shalt not nest complex tasks!");
                    }
				}//end of inner-if
			}
		}//end of outer-if
    	if(wError)
    	{
    		throw new Exception("It's only the Complex tasks to which you shall add sub-tasks !");
    	}
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void undo()
    {
        if(ParentTask != null && !undone)
		{
            ParentTask.removeTask(NewSubTask); //removeTask removes based on Memory Reference Addresse.
            undone=true;
        }
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void redo()
    {
        if(ParentTask != null && undone)
		{
            ParentTask.addTodoItem(NewSubTask);
            undone=false;
        }
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
}
